package com.ktmall.ktmalluijava.controller;

import com.ktmall.ktmalluijava.pojo.Attr;
import com.ktmall.ktmalluijava.service.AttrService;
import com.ktmall.ktmalluijava.utils.Page;
import com.ktmall.ktmalluijava.utils.ResMsg;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/5/15 下午8:00
 */
@RestController
@RequestMapping("/attr")
public class AttrController {

    @Resource
    private AttrService attrService;

    @GetMapping("/list")
    public String select(String name,Integer pageNo, Integer pageSize){
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 5 : pageSize;
        Page<Attr> page = attrService.getPage(name,pageNo,pageSize);
        return ResMsg.success("规格属性列表加载成功",page);
    }

    @PostMapping("/add")
    public String add(@RequestBody Attr attr){
        int i = attrService.add(attr);
        if(i>0){
            return ResMsg.success("新增成功");
        }else{
            return ResMsg.error("新增失败");
        }
    }

    @GetMapping("/detail")
    public String detail(Integer id){
        Attr attr = null;
        try{
            attr = attrService.detail(id);
        }catch(Exception e){
            return ResMsg.error("规格属性详情加载失败",e);
        }
        return ResMsg.success("规格属性详情加载成功",attr);
    }

    @PostMapping("/update")
    public String update(@RequestBody Attr attr){
        int i = attrService.update(attr);
        if(i>0){
            return ResMsg.success("修改成功");
        }else{
            return ResMsg.error("修改失败");
        }
    }

    @PostMapping("/del")
    public String delete(@RequestBody Integer... id){
        int i = attrService.delete(id);
        if(i>0){
            return ResMsg.success("删除成功");
        }else{
            return ResMsg.error("删除成功");
        }
    }

    @GetMapping("/all")
    public String getAll(){
        return ResMsg.success("全部规格属性加载成功",attrService.getAll());
    }
}
